﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows;

namespace DBSync.Util
{
    class SyncConnection
    {
        private static SyncConnection instance;

        public bool InitialSet { get; set; }
        public string Host { get; set; }
        public int Port { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string PHRDbPath { get; set; }
        public string DCDRDbPath { get; set; }
        public bool ValidPHRDb { get; set; }
        public bool ValidDCDRDb { get; set; }

        public static SyncConnection Instance { 
            get{
                if (instance == null)
                {
                    instance = new SyncConnection();
                }
                return instance;
            } 
        }

        public static void updateAppSettings()
        {
            if (instance != null)
            {
                try
                {
                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    var settings = config.AppSettings.Settings;
                    settings[SyncKeys.INITIALSET].Value = instance.InitialSet.ToString();
                    settings[SyncKeys.HOST].Value = instance.Host;
                    settings[SyncKeys.PORT].Value = instance.Port.ToString();
                    settings[SyncKeys.USERNAME].Value = instance.Username;
                    settings[SyncKeys.PASSWORD].Value = instance.Password;
                    settings[SyncKeys.PHRDBPATH].Value = instance.PHRDbPath;
                    settings[SyncKeys.DCDRDBPATH].Value = instance.DCDRDbPath;
                    settings[SyncKeys.VALIDDCDRDB].Value = instance.ValidDCDRDb.ToString();
                    settings[SyncKeys.VALIDPHRDB].Value = instance.ValidPHRDb.ToString();


                    config.Save();
                    ConfigurationManager.RefreshSection("appSettings");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        public static void updateConnectionStrings()
        {
            try
            {
                SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
                scsb.DataSource = instance.Host + "\\SQLEXPRESS";
                scsb.InitialCatalog = SyncKeys.PHRDBNAME;
                if (instance.Host == "." && instance.Username == "")
                {
                    scsb.IntegratedSecurity = true;
                }
                else
                {
                    if (instance.Username != "" && instance.Username != null)
                    {
                        scsb.UserID = instance.Username;
                    }
                    if (instance.Password != "" && instance.Username != null)
                    {
                        scsb.Password = instance.Password;
                    }

                }

                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");

                connectionStringsSection.ConnectionStrings["DBSync.Properties.Settings.ServerpersonalhrConnectionString"].ConnectionString = scsb.ToString();

                scsb.InitialCatalog = SyncKeys.DCDRDBNAME;
                connectionStringsSection.ConnectionStrings["DBSync.Properties.Settings.ServerdrcardriveConnectionString"].ConnectionString = scsb.ToString();

                scsb = new SqlConnectionStringBuilder();
                if (instance.PHRDbPath == null)
                {
                    instance.PHRDbPath = "personalhr.sdf";
                }
                scsb.DataSource = instance.PHRDbPath;
                connectionStringsSection.ConnectionStrings["DBSync.Properties.Settings.ClientpersonalhrConnectionString"].ConnectionString = scsb.ToString();

                scsb = new SqlConnectionStringBuilder();
                if (instance.DCDRDbPath == null)
                {
                    instance.DCDRDbPath = "drcardrive.sdf";
                }
                scsb.DataSource = instance.DCDRDbPath;
                connectionStringsSection.ConnectionStrings["DBSync.Properties.Settings.ClientdrcardriveConnectionString"].ConnectionString = scsb.ToString();
                config.Save();
                ConfigurationManager.RefreshSection("connectionStrings");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        

        
        
    }

    

    
}
